package com.limedia.explorer.centralcontrol;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.limedia.explorer.utils.CommonUtils;
import com.thunder.rebot.rebotclient.R;
import com.thunder.rebot.rebotclient.activity.BaseActivity;
import com.thunder.rebot.rebotclient.activity.MajorActivity;

public class InputIpActivity extends BaseActivity implements OnClickListener {

	private EditText routOneIp;
	private Button confirm;
	private final String TAG = "InputIpActivity";
	private SharedPreferences sharedPreferences;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_inputip);
		sharedPreferences = getSharedPreferences("ip", MODE_PRIVATE);
		initView();
		setListener();
	}

	private void setListener() {
		confirm.setOnClickListener(this);
	}

	private void initView() {
		routOneIp = (EditText) findViewById(R.id.ip_first);
		confirm = (Button) findViewById(R.id.btn_ok);
	}

	@Override
	protected void onResume() {
		super.onResume();
		
        String firstIpvalue = sharedPreferences.getString("FirstIpAddress", "1");
        Log.e(TAG, "onResume:Address1"+firstIpvalue);
		routOneIp.setText(firstIpvalue);
		if("1".equals(routOneIp.getText().toString())){
			routOneIp.setText("192.168.1.1");
		}
	}
	@Override
	public void onClick(View v) {
		String FirstIpAddress = routOneIp.getText().toString().trim();
		boolean isFirstIpValid = CommonUtils.isIpAddressValid(FirstIpAddress);
		if(isFirstIpValid){   //都是可用的ip
			//保存ip
			Editor editor = sharedPreferences.edit();
            editor.putString("FirstIpAddress", FirstIpAddress);
            editor.commit();
			startActivity(new Intent(getApplicationContext(), ControlActivity.class));
		}else{
			Toast.makeText(getApplicationContext(), "您输入的ip格式不正确，请重新输入",Toast.LENGTH_SHORT).show();
		}
	}

}
